package com.se.common;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.se.common.Constant.*;

public class Group {
    private int groupId;
    private String groupName;
    private String groupOwner;
    private final List<String> groupMembers = new ArrayList<>();
    //ID+空格+NAME
    private String groupAI;
    private boolean isNotify;
    private final List<String> notifyList = new ArrayList<>();

    public void setNotify(boolean bool) {
        isNotify = bool;
    }

    public boolean isNotify() {
        return isNotify;
    }

    public void resetNotifyList() {
        notifyList.clear();
        notifyList.add(groupOwner);
        notifyList.addAll(groupMembers);
    }

    public List<String> getNotifyList() {
        return notifyList;
    }

    public String getNotifyListAsString() {
        StringBuilder re = new StringBuilder();
        for(var i : notifyList) {
            re.append(i);
            re.append(" ");
        }

        return re.toString();
    }

    public void setNotifyListByString(String str) {
        if(str == null || str.isEmpty()) {
            return;
        }
        List<String> list = List.of(str.split(" "));
        notifyList.clear();
        notifyList.addAll(list);
    }

    public void rmNotify(String username) {
        notifyList.remove(username);
    }

    public void addGroupMember(String username) {
        groupMembers.add(username);
    }

    public List<String> getGroupMembers() {
        return groupMembers;
    }

    public String getGroupMembersAsString() {
        StringBuilder re = new StringBuilder();
        for(var i : groupMembers) {
            re.append(i);
            re.append(" ");
        }
        return re.toString();
    }

    public void setGroupMembersByString(String str) {
        String[] list = str.split(" ");
        groupMembers.clear();
        groupMembers.addAll(new ArrayList<>(List.of(list)));
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public String getGroupOwner() {
        return groupOwner;
    }

    public void setGroupOwner(String groupOwner) {
        this.groupOwner = groupOwner;
    }

    public String getGroupAI() {
        return groupAI;
    }

    public void setGroupAI(String groupAI) {
        this.groupAI = groupAI;
    }

    public String toString() {
        JSONObject json = new JSONObject();
        json.put(GROUP_ID, groupId);
        json.put(GROUP_NAME, groupName);
        json.put(GROUP_OWNER, groupOwner);
        json.put(GROUP_MEMBER, getGroupMembersAsString());
        json.put(GROUP_AI, groupAI);
        json.put(NOTIFY_LIST, getNotifyListAsString());

        return json.toString();
    }

    public List<String> getAllUsers() {
        List<String> list = new ArrayList<>();
        list.add(groupOwner);
        list.addAll(groupMembers);
        return list;
    }

    public Group() {
    }

    public Group(String str) {
        JSONObject json  = new JSONObject(str);
        if(!json.isNull(GROUP_ID)) {
            setGroupId(json.getInt(GROUP_ID));
        }
        setGroupName(json.getString(GROUP_NAME));
        setGroupOwner(json.getString(GROUP_OWNER));
        if(!json.isNull(GROUP_MEMBER)){
            setGroupMembersByString(json.getString(GROUP_MEMBER));
        }
        if(!json.isNull(GROUP_AI))
        {
            setGroupAI(json.getString(GROUP_AI));
        }
        if(!json.isNull(NOTIFY_LIST)) {
            setNotifyListByString(json.getString(NOTIFY_LIST));
        }
    }
}
